package com.example.service.impl;

import com.example.dao.UserDao;
import com.example.dao.impl.UserDaoimpl;
import com.example.dto.UserDto;
import com.example.entity.User;
import com.example.service.UserService;


public class UserServiceimpl implements UserService {
    private UserDao userDao = new UserDaoimpl();

    @Override
    public UserDto login(String username, String password) {
        User user = this.userDao.findByUsername(username);
        UserDto userDto = new UserDto();
        if(user == null){
            userDto.setMsg("用户名不存在");
        }else {
            if(!user.getPassword().equals(password)){
                userDto.setMsg("密码错误");
            }else{
                userDto.setMsg("登录成功");
                userDto.setUser(user);
            }
        }
        return userDto;
    }

    @Override
    public UserDto register(String username, String password) {
        User user = this.userDao.findByUsername(username);
        UserDto userDto = new UserDto();
        if(user == null){
            userDto.setMsg("注册成功");
            return userDto;
        }else {
            userDto.setMsg("用户名已存在");
            return userDto;
        }
    }

    @Override
    public UserDto changePassword(String username, String oldPassword, String newPassword) {
        User user = this.userDao.findByUsername(username);
        UserDto userDto = new UserDto();
        if(user == null){
            userDto.setMsg("用户名不存在");
        }else {
            if(!user.getPassword().equals(oldPassword)){
                userDto.setMsg("密码错误");
            }else{
                userDto.setMsg("密码修改成功");
                userDto.setUser(this.userDao.updatePassword(username,newPassword));
            }
        }
        return userDto;
    }
}
